package com.relife.sys.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.relife.common.utils.SmsUtil;
import com.relife.core.entity.SmsEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.relife.common.utils.StringUtil;
import com.relife.common.model.AjaxJson;
import com.relife.common.model.Page;
import com.relife.core.easyui.Easyui;
import com.relife.core.qbc.QbcQuery;
import com.relife.core.utils.ExcelUtil;

/**
 * 短信
 * 
 * @author ANT
 * @date 2016-07-01 10:47:51
 */
@Controller
@RequestMapping("sys/sms")
public class SmsController extends BaseController {

	@RequestMapping
	public String smsList(HttpServletRequest req) {
		return "sys/sms/smsList";
	}

	/**
	 * 数据列表
	 * 
	 * @author ANT
	 * @date 2016-07-01 10:47:51
	 * @param page
	 */
	@RequestMapping
	public void listData(SmsEntity sms, Page page, HttpServletResponse res, HttpServletRequest req) {
		QbcQuery qbc = new QbcQuery(sms,req.getParameterMap());
		service.getPage(qbc, page);
		Easyui.datagrid(page, res);
		ExcelUtil.exportExcel(page, qbc, "短信发送记录");
	}

	@RequestMapping
	public String sms(HttpServletRequest req) {
		JSONArray array = new JSONArray();
		StringBuffer sql = new StringBuffer();
		sql.append("select t.id,t.community_name name from pm_House_Community t where 1=1");
		this.openPermission(sql, "t");
		sql.append(" order by t.orderby asc");
		List<Map<String, Object>> list = service.getList(sql.toString());
		String url = req.getContextPath() + "/pm/houseBuilding/getHouseArea.do?id=";
		for (int i = 0; i < list.size(); i++) {
			JSONObject obj = new JSONObject();
			obj.put("id", list.get(i).get("id"));
			obj.put("name", list.get(i).get("name"));
			obj.put("href", url + list.get(i).get("id"));
			obj.put("type", 1);
			obj.put("isParent", true);
			array.add(obj);
		}
		req.setAttribute("nodes", array);
		return "sys/sms/sms";
	}

	/**
	 * 
	 * @author WU
	 * @date 2017年5月15日 下午3:38:27
	 * @param content
	 * @return
	 */
	@ResponseBody
	@RequestMapping
	public AjaxJson saveSms(String content, String buildingId, String areaId, String communityId) {
		AjaxJson j = new AjaxJson();
		try {
			List<Object> param = new ArrayList<Object>();
			String sql = "select u.phone from sys_user u left join pm_resident_house r on r.resident_id=u.id left join pm_house_info h on h.id=r.house_id where u.phone is not null";
			if (StringUtil.isNotEmpty(buildingId)) {
				sql += " and h.building_id=?";
				param.add(buildingId);
			} else if (StringUtil.isNotEmpty(areaId)) {
				sql += " and h.area_id=?";
				param.add(areaId);
			} else if (StringUtil.isNotEmpty(communityId)) {
				sql += " and h.community_id=?";
				param.add(communityId);
			}
			List<Map<String, Object>> list = service.getList(sql, param.toArray());
			for (int i = 0; i < list.size(); i++) {
				SmsUtil.sendSMS(list.get(i).get("phone").toString(), content);
			}
		} catch (Exception e) {
			j.setException(e);
			e.printStackTrace();
		}
		return j;

	}

	@RequestMapping
	public String sendSms(HttpServletRequest req) {
		return "sys/sms/sendSms";
	}

	/**
	 * 
	 * @author WU
	 * @date 2017年5月16日 下午1:57:51
	 * @param phones
	 * @param content
	 * @return
	 */
	@ResponseBody
	@RequestMapping
	public AjaxJson sendSmsSave(String phones, String content) {
		AjaxJson j = new AjaxJson();
		try {
			phones = phones.replace("，", ",").replace(";", ",").replace("；", ",");
			String[] phone = phones.split(",");
			for (int i = 0; i < phone.length; i++) {
				SmsUtil.sendSMS(phone[i], content);
			}
		} catch (Exception e) {
			j.setException(e);
			e.printStackTrace();
		}
		return j;

	}

}
